<template>
  <el-form ref="form" :rules="rules" label-width="100px">
    <el-form-item label="步骤名称" prop="name">
      <el-input v-model="step.name" placeholder="请输入步骤名称"></el-input>
    </el-form-item>
    <br>
    <el-button icon="plus" @click="_addRecord"></el-button>
    <br><br>
    <el-table :data="step.fields" stripe border :max-height="400">
      <el-table-column type="index" :width="60"></el-table-column>
      <el-table-column label="流字段" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.field_name}}</div>
          <div v-else>
            <el-input v-model="scope.row.field_name"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="计算公式" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.calc_type}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.calc_type">
              <el-option v-for="item in options.calcType" :label="item.name" :value="item.value"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="字段A" :width="140">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.field_a}}</div>
          <div v-else>
            <el-select v-model="scope.row.field_a">
              <el-option v-for="item in mergedField" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="字段B" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.field_b}}</div>
          <div v-else>
            <el-select v-model="scope.row.field_b">
              <el-option v-for="item in mergedField" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <!--  <el-table-column label="字段C" :width="120">
          <template scope="scope">
            <div v-if="!scope.row.edit">{{scope.row.field_c}}</div>
            <div v-else class="control">
              <el-input v-model="scope.row.field_c"></el-input>
            </div>
          </template>
        </el-table-column>-->
      <el-table-column label="值类型" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.value_type}}</div>
          <div v-else class="control">
            <el-select class="inline-input" v-model="scope.row.value_type">
              <el-option v-for="item in options.inputTypes" :value="item"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="控制" :width="140">
        <template scope="scope">
          <el-row>
            <el-col :span="12"><el-button size="small" type="info" @click="reverseEditState(scope.$index, scope.row)">编辑</el-button></el-col>
            <el-col :span="12"><el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button></el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
</template>

<script>
  import {Steps, Options} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";

  /**
   * 计算器step 输出fields.field_name
   * */
  export default{
    mixins: [StepCommon],
    computed:{
      mergedField(){

        return this.step.availableFields.concat(this.step.fields.map(el=>({name:el.field_name,step:this.step.name}))).filter(el=>el.name!=="");
      }
    },
    data(){
      return {
        step: Steps.Calculator(),
        options: Options
      }
    },
    methods: {
      updateFields(){
        this.step.outputFields = this.step.fields.map(el => el.field_name);
      }
    }
  }
</script>
